package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursing_project")
@Api(value = "护理项目管理")
public class NursingProjectController extends BaseController{

    @Autowired
    private NursingProjectService nursingProjectService;

    /**
     * 根据条件 分页查询护理项目
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @ApiOperation(value = "根据条件分页查询护理项目")
    public ResponseResult<PageResponse<NursingProjectVo>> findByPage(
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "status",required = false) Integer status,
            @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize
    ){
        PageResponse<NursingProjectVo> page = nursingProjectService.findByPage(name,status,pageNum,pageSize);
        return success(page);
    }

    /**
     * 新增护理项目
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增护理项目")
    public ResponseResult save(@RequestBody NursingProjectDto dto){
//        属性拷贝
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProjectService.save(nursingProject);
        return success();
    }

    /**
     * 根据id 查询护理项目
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询护理项目")
    public ResponseResult<NursingProjectVo> findById(@PathVariable(name = "id") Long id){
        NursingProjectVo vo = nursingProjectService.findById(id);
        return success(vo);
    }

    /**
     * 编辑护理项目
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "编辑护理项目")
    public ResponseResult updateById(@RequestBody NursingProjectDto dto){
//        属性拷贝
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProjectService.updateById(nursingProject);
        return success();
    }

    /**
     * 根据id 启用禁用状态
     * @param id
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "根据id启用禁用状态")
    public ResponseResult updateStatus(@PathVariable(name = "id") Long id,
                                       @PathVariable(name = "status") Integer status){
        nursingProjectService.updateStatus(id,status);
        return success();
    }

    /**
     * 根据id 删除护理项目
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除护理项目")
    public ResponseResult deleteById(@PathVariable(name = "id") Long id){
        nursingProjectService.deleteById(id);
        return success();
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有护理项目")
    public ResponseResult<List<NursingProjectVo>> findAll(){
        List<NursingProjectVo> voList = nursingProjectService.findByAll();
        return success(voList);
    }
}
